﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Other
 * 唯一标识：29ef6198-a30a-4101-b801-0d075d62d8be
 * 文件名：TimeLine
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/7/25 10:18:32
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 时间线控件
/// </summary>
public sealed class TimeLine : ControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal TimeLine()
    {
    }

    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<el-timeline style=\"max-width: {MaxWidth}px\">");
        foreach (var item in TimeLineItems)
        {
            sb.AppendLine($"<el-timeline-item timestamp=\"{item.Timestamp}\" placement=\"{item.PlacementType.GetDescription()}\">");

            if (item.Content is string content)
            {
                sb.AppendLine(content);
            }
            else if (item.Content is ControlBase cb)
            {
                sb.AppendLine(cb.ConverToHtml());
            }

            sb.AppendLine($"</el-timeline-item>");
        }
        sb.AppendLine($"</el-timeline>");

        return sb.ToString();
    }

    /// <summary>
    /// 时间线控件最大宽度
    /// </summary>
    internal int MaxWidth { get; set; }

    internal List<TimeLineItem> TimeLineItems { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建时间轴控件
    /// </summary>
    /// <param name="maxWidth"></param>
    /// <param name="timeLineItems"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static TimeLine CreateTimeLine(int maxWidth, Action<List<TimeLineItem>> timeLineItems)
    {
        TimeLine timeLine = new TimeLine()
        {
            Id = GenerateId(),
            Title = "时间轴控件",
            MaxWidth = maxWidth,
            TimeLineItems = new List<TimeLineItem>()
        };

        timeLineItems?.Invoke(timeLine.TimeLineItems);

        return timeLine;
    }
}

/// <summary>
/// 时间戳位置
/// </summary>
public enum TimeLinePlacementTypeEnum
{
    /// <summary>
    /// 上部
    /// </summary>
    [Description("top")]
    Top,

    /// <summary>
    /// 下部
    /// </summary>
    [Description("bottom")]
    Bottom,
}

/// <summary>
/// 时间轴子控件
/// </summary>
public sealed class TimeLineItem
{
    /// <summary>
    /// 内容
    /// </summary>
    public object Content { get; set; }

    /// <summary>
    /// 时间戳
    /// </summary>
    public string Timestamp { get; set; }

    /// <summary>
    /// 时间戳位置
    /// </summary>
    public TimeLinePlacementTypeEnum PlacementType { get; set; }
}